package cz.zcu.fav.rat.managed.bean;


import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import cz.zcu.fav.rat.beans.ObjectClass;
import cz.zcu.fav.rat.dataaccess.service.ObjectClassService;
import cz.zcu.fav.rat.managed.locale.LocaleMessageFactory;

@ManagedBean
@ViewScoped
public class EditObjectClassBean extends AbstractEditBean
{
	private static final long serialVersionUID = -7457847150329648128L;

	private ObjectClass selectedObjectClass;

	@PostConstruct
	public void init()
	{
		super.init();
		if (id != null)
		{
			selectedObjectClass = getObjectClassService().get(id);
		} else
		{
			selectedObjectClass = new ObjectClass();
		}
	}

	private ObjectClassService getObjectClassService()
	{
		return SpringBeanFactory.getInstance().<ObjectClassService> getBean(
				"objectClassService", ObjectClassService.class);
	}

	public void create()
	{
		getObjectClassService().create(selectedObjectClass);

		selectedObjectClass = new ObjectClass();
		String message = LocaleMessageFactory
				.getLocale("view.objectcreate.info.objectcreated");

		FacesContext.getCurrentInstance().addMessage("form",
				new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
	}

	public String edit()
	{
		getObjectClassService().update(selectedObjectClass);
		return "objectclasstable?faces-redirect=true";
	}

	public ObjectClass getSelectedObjectClass()
	{
		return selectedObjectClass;
	}

	public void setSelectedObjectClass(ObjectClass selectedObjectClass)
	{
		this.selectedObjectClass = selectedObjectClass;
	}

}
